package org.example.service.File;

import lombok.extern.log4j.Log4j;

import java.io.File;

@Log4j
public class DeleteDoc {

    public static boolean deleteFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            log.error("文件删除失败，文件不存在");
            return false;
        }
        if (!file.isDirectory()) {
            log.error("文件删除失败，路径不是一个目录");
            return false;
        }
        return deleteDirectory(file);
    }

    private static boolean deleteDirectory(File directory) {
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteDirectory(file);
                } else {
                    if (!file.delete()) {
                        log.error("文件删除失败: " + file.getAbsolutePath());
                        return false;
                    }
                }
            }
        }
        if (!directory.delete()) {
            log.error("目录删除失败: " + directory.getAbsolutePath());
            return false;
        }
        return true;
    }


}
